package com.jule.goldfast.game.service.holder;

import com.jule.goldfast.game.dao.DBUtil;
import com.jule.goldfast.game.dao.bean.RoomConfigModel;
import lombok.extern.slf4j.Slf4j;
import java.util.*;

@Slf4j
public class RoomConfigHolder {
    private static Map<String, RoomConfigModel> ROOM_CONFIG = new HashMap<>(); //所有房间配置

    private static class SingletonHolder {
        protected static final RoomConfigHolder instance = new RoomConfigHolder();
    }

    public static final RoomConfigHolder getInstance() {
        return RoomConfigHolder.SingletonHolder.instance;
    }

    private RoomConfigHolder() {
        init();
    }

    /**
     * @param roomId
     * @return
     */
    //获取桌子的所有信息
    public RoomConfigModel getRoomConfig(String roomId) {
        return ROOM_CONFIG.get(roomId);
    }

    /**
     * 初始化内存
     */
    public boolean init(){
        try{
            List<RoomConfigModel> configList = DBUtil.getRoomConfigFromDb();
            for(int i=0; i < configList.size(); i++){
                RoomConfigModel model = configList.get(i);
                ROOM_CONFIG.put(model.getRoomId(), model);
                log.debug("加载房间："+model.toString());
            }
            log.info("加载 " + ROOM_CONFIG.size() + " 个房间配置");
            return true;
        }catch(Exception ex){
            log.error(ex.getMessage());
        }
        return false;
    }
}
